package com.winchain.chainbill.bill.biz.discount.rx;

import com.shell.bee.base.exception.UnCheckMsgException;
import com.shell.bee.base.utils.DateUtil;
import com.shell.bee.base.utils.StringUtil;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.winchain.chainbill.basic.activity.po.BasCpnRBiz;
import com.winchain.chainbill.basic.activity.po.BasCusCpn;
import com.winchain.chainbill.basic.activity.service.BasCpnRBizService;
import com.winchain.chainbill.basic.activity.service.BasCusCpnService;
import com.winchain.chainbill.bill.base.po.CbDct;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.bill.biz.BillEvent;
import com.winchain.chainbill.bill.biz.transfer.vo.BillSplitEvent;
import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillDctCpnSubscriber extends OnErrorNotImplementedSubscriber<BillEvent<CbDct>> {
	@Autowired
	private BasCusCpnService basCusCpnService;
	@Autowired
	private BasCpnRBizService basCpnRBizService;

	public void onApplicationEvent(BillEvent<CbDct> t) {
		CbDct cbDct = (CbDct) t.getBill().getSource();
		Iterator localIterator;
		if ((t instanceof BillSplitEvent)) {
			BillSplitEvent<CbDct> event = (BillSplitEvent) t;

			List<Bill<CbDct>> bills = (List<Bill<CbDct>>) event.getChildren();
			Bill<CbDct> bill;
			for (localIterator = bills.iterator(); localIterator.hasNext();) {
				bill = (Bill) localIterator.next();
				cbDct = (CbDct) bill.getSource();
				if ((cbDct != null) && (StringUtil.notEmpty(cbDct.getDctId()))) {
					break;
				}
			}
		}
		if ((cbDct == null) || (cbDct.getDctId() == null)) {
			return;
		}
		String selectCpnsIds = cbDct.getSelectCpnsIds();
		BigDecimal deductInt = new BigDecimal(0);
		if (StringUtil.notEmpty(selectCpnsIds)) {
			String[] ids = selectCpnsIds.split(",");
			for (String id : ids) {
				BasCusCpn cpn = (BasCusCpn) this.basCusCpnService.selectByKey(id);
				if (!cpn.getCpnSts().equals("1")) {
					throw new UnCheckMsgException("������������������������������");
				}
				deductInt = deductInt.add(cpn.getCpnAmt());
				if (deductInt.compareTo(cbDct.getDctInt()) <= 0) {
					cpn.setUsedAmt(cpn.getCpnAmt());
					cpn.setUsedDate(DateUtil.getSystemDate());
					cpn.setCpnSts("2");
				} else {
					cpn.setUsedAmt(cbDct.getDctInt().add(cpn.getCpnAmt()).subtract(deductInt));
					cpn.setUsedDate(DateUtil.getSystemDate());
					cpn.setCpnSts("2");
				}
				BasCpnRBiz bcb = new BasCpnRBiz();
				bcb.setBillId(cbDct.getBillId());
				bcb.setBizId(cbDct.getDctId());
				bcb.setBizType("1");
				bcb.setCusCpnId(cpn.getCusCpnId());
				this.basCpnRBizService.save(bcb);

				this.basCusCpnService.updateNotNull(cpn);
				if (deductInt.compareTo(cbDct.getDctInt()) >= 0) {
					deductInt = cbDct.getDctInt();
					break;
				}
			}
			cbDct.setDeductInt(deductInt);
		}
	}
}
